#include "qiconbutton.h"
#include <QPainter>
QIconButton::QIconButton(int nID, QString qsIconUrl, QSize size, QWidget *parent)
    : QWidget{parent}
    , m_qsIconUrl(qsIconUrl)
    , m_image(nullptr)
    , m_nID(nID)
    , m_status(E_NORMAL)
    , m_eModel(E_NORMALMODEL)
    , m_bPressed(false)
    , m_bSelected(false)
{
    m_image.load(qsIconUrl);
    m_image = m_image.scaled(size.width() / 3, size.height() / 3);
    m_iamgeScale = m_image.scaled(m_image.width()*2 / 3, m_image.height()*2 / 3);
    setFixedSize(size.width(), size.height());
}

void QIconButton::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QSize size = this->size();
    QSize imageSize = m_image.size();

    // 计算居中位置
    int x = (size.width() - imageSize.width()) / 2;
    int y = (size.height() - imageSize.height()) / 2;

    int x1 = (size.width() - m_iamgeScale.width())/ 2;
    int y1 = (size.height() - m_iamgeScale.height())/ 2;

    if(m_status == E_FOCUSIN)
    {
        QBrush brush(QColor(47, 45, 52, 100));
        QPen pen(QColor(47, 45, 52, 100));
        // 设置线宽为2px
        pen.setWidth(1);
        // 应用pen到painter
        painter.setPen(pen);
        painter.setBrush(brush);
        painter.drawRect(QRect(0, 0, width(), height()));
    }

    if(m_bSelected)
    {
        QBrush brush(QColor(243, 128, 100));
        painter.fillRect(QRect(x + 5, y + m_image.height() + 5, imageSize.width() - 5, 5), brush);
    }

    if(m_bPressed)
    {
        QBrush brush(QColor(243, 128, 100));
        // QPen pen(QColor(243, 128, 100, 100));
        // // 设置线宽为2px
        // pen.setWidth(1);
        // // 应用pen到painter
        // painter.setPen(pen);
        // painter.setBrush(brush);
        painter.fillRect(QRect(x + 5, y + m_image.height() + 5, imageSize.width() - 5, 5), brush);
        painter.drawImage(QPoint(x1, y1), m_iamgeScale);
    }
    else if(m_status == E_SELECTEDNOFOCUS)
    {

    }
    else
    {
        painter.drawImage(QPoint(x, y), m_image);
    }
}

void QIconButton::addIcon(QString qsIconUrl)
{
    m_qsIconUrl = qsIconUrl;
    m_image.load(qsIconUrl);
}

void QIconButton::setModel(eModel model)
{
    m_eModel = model;
}

int QIconButton::getId()
{
    return m_nID;
}

bool QIconButton::hasSelected()
{
    return m_bSelected;
}

void QIconButton::setSelected(bool bSelected)
{
    m_bSelected =bSelected;
    update();
}

void QIconButton::setBtnStatus(eBtnStatus eStatus)
{
    m_status = eStatus;
}

void QIconButton::enterEvent(QEnterEvent *event)
{
    m_status = E_FOCUSIN;
    update();
}

void QIconButton::leaveEvent(QEvent *event)
{
    m_status = E_NORMAL;
    update();
}

void QIconButton::mousePressEvent(QMouseEvent *event)
{
    m_bPressed = true;
    if(m_eModel != E_THREESTATUSMODEL)
    {
        m_bSelected = true;
    }
    else
    {
        m_bSelected = !m_bSelected;
    }

    m_status = E_PRESSED;
    emit sigPressed(m_nID);
    update();
}

void QIconButton::mouseReleaseEvent(QMouseEvent *event)
{
    m_bPressed = false;
    if(m_eModel != E_THREESTATUSMODEL)
    {
        m_bSelected = false;
    }
    update();
}
